/**
 * Created by SPG on 2016/7/1.
 */
$(function () {
    //登录框
    var $modal = $('#myLoginModal');
    //$modal.modal();
    var login_name_dvi = $("#login_name_dvi");
    var login_pwd_dvi = $("#login_pwd_dvi");
    //登录
    $("#login_button").click(function(){
        var loginBaen = {};
        loginBaen.loginName=$("#login_name_dvi > input").val();
        loginBaen.loginPwd=$("#login_pwd_dvi > input").val();
        $.ajax({
            cache: false,
            dataType: "json",
            type: "POST",
            url: '/pipes/user/login',
            data: {
                "login": privateJsonString(loginBaen)
            },
            error: function () {//请求失败处理函数
                loginAlert("请求出错");
            },
            success: function (data) {
                if(data.success){
                    $('#myLoginModal').modal('close');
                }else{
                    loginAlert(data.message);
                }
            }
        });
    });

    function loginAlert(msg){
        $('#login_button').popover('setContent', msg);
        $('#login_button').popover('open');
    };

    //菜单加载
    var $munes = $("#admin-offcanvas").find(".admin-sidebar-list");//菜單
    var $content =$("#admin-content");//內容存放處
    var htm = [];//內容存放處
    //var $tsa = $(this);//菜單當前點擊
    $.ajax({
        cache: false,
        dataType: "json",
        type: "POST",
        url: '/pipes/menus/urls',
        error: function () {//请求失败处理函数
            alert("请求出错！");
        },
        success: function (data) {
            var defaultHtmUrl = data[0].url;
            for (var i=0;i<data.length;i++){
                if(i==0){
                    htm.push('<li><a href="#" url="'+data[i].url+'"><span class="'+data[i].icon+'"></span><span class="am-icon-angle-right am-fr am-margin-right"></span>'+data[i].titel+'</a></li>');
                }else{
                    htm.push('<li><a href="#" url="'+data[i].url+'"><span class="'+data[i].icon+'"></span>'+data[i].titel+'</a></li>');
                }
            }
            $munes.html(htm);
            $.ajax({
                url:defaultHtmUrl,
                dataType:"html",
                success: function(str){
                    $content.html(str);
                }
            });

            $munes.find("a").click(function(){
                var $tsa=$(this);
                $.ajax({
                    url:$(this).attr("url"),
                    dataType:"html",
                    success: function(str){
                        $content.html(str);
                    }
                });
            })
        }
    });



});